﻿using System.Collections.Generic;
using System.Windows.Media;
using MyFloridaHouse.libSvOpenDistricts.MapShapes;
using MyFloridaHouse.ServiceConn.DistrBldrSvcRef;
using System.Collections.ObjectModel;
using System;
using MyFloridaHouse.libSvOpenDistricts.SummaryDataGrid;

namespace MyFloridaHouse.libSvOpenDistricts.Districts
{
    public class DistrictFill : RegionFillBase
    {
        public DistrictFill()
            : base()
        {
            DistrictColors.DistrictColorConditionsChanged += OnDistrictColorConditionsChanged;
        }

        private bool _showDistrictsPopDevShading;
        public bool showDistrictsPopDevShading
        {
            get { return _showDistrictsPopDevShading; }
            set
            {
                if (_showDistrictsPopDevShading != value)
                {
                    _showDistrictsPopDevShading = value;
                    RaiseRegionFillConditionsChanged(this);
                    NotifyPropertyChanged("showDistrictsPopDevShading");
                }
            }
        }

        private int maxPopulationDeviation = 0;
        private int minPopulationDeviation = 0;

        /// <summary>
        /// DistrictFill allows shading of districts based on population deviation
        /// </summary>
        /// <param name="regionData"></param>
        /// <returns></returns>
        public override SolidColorBrush GetRegionFillColorBrush(int districtID, List<RegionDataDTO> regionData)
        {
            // regionData is empty for districts,  District data is stored in the SummaryDataGridManager

            SolidColorBrush regionFillColor = new SolidColorBrush(Colors.Transparent);
            if (showFill)
            {
                if (showDistrictsPopDevShading)
                {
                    int populationDeviation = SummaryDataGridManager.GetPopulationDeviation(districtID);
                    int percentDeviation = 0;
                    if (populationDeviation > 1500) { percentDeviation = 90; }
                    else if (populationDeviation > 1000) { percentDeviation = 80; }
                    else if (populationDeviation > 500) { percentDeviation = 70; }
                    else if (populationDeviation > 100) { percentDeviation = 100; }
                    else if (populationDeviation > 1) { percentDeviation = 60; }
                    else if (populationDeviation >= 0) { percentDeviation = 50; }
                    else if (populationDeviation > -100) { percentDeviation = 100; }
                    else if (populationDeviation > -500) { percentDeviation = 40; }
                    else if (populationDeviation > -1000) { percentDeviation = 30; }
                    else if (populationDeviation > -1500) { percentDeviation = 20; }
                    else { percentDeviation = 0;}

                    if (percentDeviation == 50)
                    {
                        return new SolidColorBrush(Colors.Gray);
                    }
                    else
                    {
                        return GetPercentFillBrush(percentDeviation, DistrictColors.districtColorsInstance.opacity);
                    }
                }
                else
                {
                    return DistrictColors.GetBrushFromIndex(districtID);
                }
            }
            return regionFillColor;
        }

        public void OnDistrictColorConditionsChanged()
        {
            RaiseRegionFillConditionsChanged(this);
        }


        // this takes the place of the aggregateLayerData method as used by normal layers
        public void OnSummaryDataGridDataChanged(EventArgs args)
        {
            RaiseRegionFillConditionsChanged(this);
        }
    }
}
